* Cohesive Institutions and Political Violence
* Thiemo Fetzer and Stephan Kyburz

use "data files/NGA.PAN.MERGED.L7.dta",clear

merge m:1 ID_lgc year month using "data files/NGGEOPRECISE.dta"
keep if _merge==3
drop _merge

merge m:1 ID_lgc year month using "data files/NONBOKOHARAM.dta"
keep if _merge==3
drop _merge



merge m:1 ID_2 year month using "data files/LGC_ELEC_FILLED.dta"

keep if _merge==3
drop _merge 


merge m:1 year using "data files//NGABUDGETOILPRICE.dta"
keep if _merge==1 | _merge==3
drop _merge

gen quarter = 1 
replace quarter = 2 if month>3 & month<=6
replace quarter = 3 if month>6 & month<=9
replace quarter = 4 if month>9 & month<=12

cap drop _merge
merge m:1 ID_lgc using "data files/FAAC_allocation_indices_2006.dta"


replace lgc_elec_status  = naloclgcelec if lgc_elec_status==.

drop if state=="TOTAL"

drop _merge

egen stm = group(state tm)
*governor FE
egen stmgov = group(state st_governor )
*We could also use the variable state here.
egen lgcstategov = group(ID_lgc  st_IDgovernor )
egen idelec = group(ID_lgc lgc_elec_status )


run "stata do/codingdiscrepancies.do"

*****
*****
**
** SECTION: Produces summary statistics summarizing formula weights
**
*****
*****

label variable totalindex_weight "Total index"
label variable equality  "Equality"
label variable population_weight "Population"
label variable reveffort_weight_25pof10p "Internal Revenue Generation effort"
label variable landmass_weight_50pof10p "Landmass"
label variable terrain_weight_50pof10p "Terrain"
label variable hospital_beds_30pof10p "Health - Hospital Beds"
label variable primary_enrollment_weight_40pof "Education - Primary enrolment"
label variable water_supply_spread_weight "Rain - Water supply spread"
label variable rainfall_proportion_weight_15po "Rain - Rainfall share"


preserve

foreach var in equality population_weight landmass_weight_50pof10p terrain_weight_50pof10p reveffort_weight_25pof10p reveffort_weight_75p primary_enrollment_weight_40pof hospital_beds_30pof10p water_supply_spread_weight rainfall_proportion_weight_15po totalindex_weight {

replace `var' = `var'*1000

}


sutex equality population_weight reveffort_weight_25pof10p landmass_weight_50pof10p terrain_weight_50pof10p hospital_beds_30pof10p primary_enrollment_weight_40pof water_supply_spread_weight rainfall_proportion_weight_15po totalindex_weight if tm==666, labels
restore


gen combinedsocio = reveffort_weight_25pof10p+ hospital_beds_30pof10p+  primary_enrollment_weight_40pof+ water_supply_spread_weight

foreach var in combinedsocio population_weight reveffort_weight_25pof10p landmass_weight_50pof10p terrain_weight_50pof10p hospital_beds_30pof10p primary_enrollment_weight_40pof water_supply_spread_weight rainfall_proportion_weight_15po totalindex_weight {

local lab =  substr("`var'",1,14)

xtile qq`lab' = `var', nq(10)

egen qq_`lab'_tm = group(qq`lab' tm)

}



***This converts allocations to dollars as of Dec 2017 exchange rate
replace lgc_alloc_mo = lgc_alloc_mo *0.0033 
replace lgc_alloc_mo = lgc_alloc_mo/1000000 

foreach var in lgc_alloc_mo_RAW lgc_extra_alloc_mo lgc_gsa_mo lgc_vat_mo {

replace `var' = `var'  *0.0033  /1000000
}

xtset ID_lgc tm 

gen logchirps = log(chirps)
gen logtemp = log(temp)
gen lgc_elec_status_logtemp= lgc_elec_status *logtemp
gen lgc_elec_status_logchirps= lgc_elec_status *log(chirps)

*****
*****
**
** SECTION: Defines different conflict outcome measures  
**
*****
*****


gen anysoleprotesterevts = soleprotesterevts > 0 if soleprotesterevts !=.
gen anypolmilitiavsciviliansevts = polmilitiavsciviliansevts > 0 if polmilitiavsciviliansevts !=.
gen anymilitaryvspolmilitiaevts = militaryvspolmilitiaevts  > 0 if militaryvspolmilitiaevts !=.




***DEFINE GDELT VARIABLES
replace protestgdelt = 0 if protestgdelt == . & year>=1999 & year<=2014
  
replace nongovmilprotestgdelt = 0 if nongovmilprotestgdelt == . & year>=1999 & year<=2014
gen anynongovmilprotestgdelt = nongovmilprotestgdelt>0 if nongovmilprotestgdelt!=.


foreach var in events fatalities  battles battlefatalities  riots riotfatalities  civilianviolence  civilianviolencefatalities civilianmilitiaviolence civilianpoliceforceviolence  polmilitiavsciviliansevts soleprotesterevts militaryvspolmilitiaevts militaryevents rebelsevents politicalmilitiaevents communalmilitiaevents riotevents protestevents protestgdelt {
local lab: variable label `var' 
gen `var'pc = `var'/((lgc_pop_2006/100000))
label variable `var'pc "`lab'"
}     
     
foreach var in events  militaryevents rebelsevents politicalmilitiaevents communalmilitiaevents riotevents protestevents protestgdelt {
local lab: variable label `var' 
gen any`var' = `var'> 0 if `var'!=.
label variable any`var' "`lab'"
}          
     
gen oilproducing = 1 if state == "AKWA IBOM" | state == "DELTA" | state=="RIVERS" | state=="BAYELSA" | state=="ONDO" | state=="EDO" | state=="IMO" | state=="ABIA"
replace oilproducing = 0 if oilproducing==.


egen temp = mean(gridpop) if year==2000, by(ID_lgc)

egen init2000pop = mean(temp), by(ID_lgc)
egen totalpop2000 = sum(init2000pop), by(year)

gen pop2000share = init2000pop/totalpop2000


egen totalpop = sum(lgc_pop_2006), by(year)
gen popshare = lgc_pop_2006 / totalpop 

label variable lgc_alloc_mo "LGC Allocations"
*tab oilgasfielddummy, gen(op_)
*label variable op_1 "No Oil/Gas field"
*label variable op_2 "Oil/Gas field"

tab oilproducing, gen(op_)
label variable op_1 "No Oil/Gas field"
label variable op_2 "Oil/Gas field"



rename brent_cr_oil_price  brent
rename ngoilpricebenchmark   ngabenchmark


gen indexbrent = totalindex * brent 
gen indexngabenchmark = totalindex * ngabenchmark
gen indexcrudeNGA = totalindex * crudeNGA
 

gen const=1
 
gen anygtdbokoharamevents = gtdbokoharamevents>0 if gtdbokoharamevents!=.
gen anynonbokoharampolmilitia = nonbokopoliticalmilitiaevents > 0 if nonbokopoliticalmilitiaevents!=.

gen anyfatalities = fatalities>0 if fatalities !=.

gen anygtdevents = gtdevents>0 if gtdevents!=.
gen anygtdnkill = gtdnkill>0 if gtdnkill!=.
gen anyged_fatalities = ged_fatalities>0 if ged_fatalities!=.

***FEDERAL CAPITAL TERRITORY WOULD OTHERWISE BE DROPPED
replace stmgov = 1000 if  state=="FEDERAL CAPITAL TERRITORY"


***SAVE TEMPORARY FILES USED FOR WATERCOLOR REGRESSIONS
save "temporary data files/TEMP.dta", replace

global controls =" "


***SAMPLE INDICATORS TO REMOVE DATA AROUND INDIVIDUAL ELECTIONS
gen sampleelectionrestricted = abs(timetoelectime)>1
gen sampleelectionrestricted2 = abs(timetoelectime)>3
gen sampleelectionrestricted3 = abs(timetoelectime)>6



***CENTER WITH MEAN ZERO
foreach var in  lgc_excl_ethnfam_share lgc_excl_ethngr_share  {

su `var'

replace `var' = `var'-`r(mean)'

} 


*I would like for the authors to show that their primary results in Table 3 are robust to (a) the exclusion of events involving Boko Haram; (b) the exclusion of states impacted by Boko Haram (Borno, Yobe, Adamawa); and (c) leaving out each individual state, to show whether their estimates depend on a particular state.

gen bokoharamstate= state=="YOBE" | state=="BORNO" | state=="ADAMAWA"
gen beforebokoharam = year<2009

*compute  allocations last 3, 6, 12 months
sort ID_lgc tm
by ID_lgc : gen alloc_agg_3m = lgc_alloc_mo+lgc_alloc_mo[_n-1] + lgc_alloc_mo[_n-2] + lgc_alloc_mo[_n-3]
by ID_lgc : gen alloc_agg_6m = alloc_agg_3m + lgc_alloc_mo[_n-4] + lgc_alloc_mo[_n-5] + lgc_alloc_mo[_n-6]
by ID_lgc : gen alloc_agg_12m = alloc_agg_6m + lgc_alloc_mo[_n-7] + lgc_alloc_mo[_n-8] + lgc_alloc_mo[_n-9]+ lgc_alloc_mo[_n-10]+ lgc_alloc_mo[_n-11]+ lgc_alloc_mo[_n-12]



sort ID_lgc tm
by ID_lgc : gen events_agg3m = events+events[_n-1] + events[_n-2] + events[_n-3]
by ID_lgc : gen events_agg6m = events_agg3m + events[_n-4] + events[_n-5] + events[_n-6]
by ID_lgc : gen events_agg12m = events_agg6m + events[_n-7] + events[_n-8] + events[_n-9]+ events[_n-10]+ events[_n-11]+ events[_n-12]




label variable indexbrent  "Index weight x Oil price"
label variable indexngabenchmark  "Index weight x Benchmark Oil price"
label variable indexcrudeNGA  "Index weight x NGA Oil production"
label variable ngabenchmark "Oil price"

label variable crudeNGA "Monthly Nigerian crude production"
label variable temperature "Temperature"


label variable oilgasfielddummy "LGA has Oil Field"
label variable oilproducing "LGA is in Oil Producing State"
label variable lgc_excl_ethngr_share "Non-aligned ethnic group"
label variable lgc_excl_ethnfam_share "Non-aligned ethnic family"

label variable events_agg3m "ACLED events (last 3 months)"
label variable events_agg6m "ACLED events (last 6 months)"
label variable events_agg12m "ACLED events (last 12 months)"

label variable alloc_agg_3m "LGC Allocations (last 3 months)"
label variable alloc_agg_6m "LGC Allocations (last 6 months)"
label variable alloc_agg_12m "LGC Allocations (last 12 months)"


label variable lgc_elec_status "Elected"
label variable temp "Temperature (annual avg)"
label variable chirps "Rainfall (annual avg)"

label variable events "Events"
label variable battles "Casualties"
label variable anyevent "Any Event"

label variable battles "Events"
label variable battlefatalities "Casualties"
label variable anybattle "Any Battle"


label variable riots "Events"
label variable riotfatalities "Casualties"
label variable anyriot "Any Riot"

label variable civilianviolence "Events"
label variable civilianviolencefatalities "Casualties"
label variable anycivilianviolence "Any Event"

label variable civilianmilitiaviolence "Events"
label variable civilianmilitiaviolencefatalits "Casualties"
label variable anycivilianmilitiaviolence "Any Militia Violence"

label variable civilianpoliceforceviolence "Events"
label variable civilianpoliceforceviolencftlts "Casualties"
label variable anycivilianpoliceforceviolence "Any Police Violence"

label variable lgc_gsa_mo "LGC Statutory Allocations"
label variable lgc_extra_alloc "LGC Extra Allocations"

rename anybattle anybattles
drop anyevent

gen anyevents = events>0 if events!=.


gen tq = yq(year,quarter)

****ESTIMATE REGRESSIONS FROM HERE ON

*this defines the table layout and the variables across column heads for the main conflict outcomes

global anywidelayout = "anyevent  anybattle anycivilianviolence anyprotestevents  anymilitaryevents anypoliticalmilitiaevents anycommunalmilitiaevents anyrebelsevents anymilitaryvspolmilitiaevts anypolmilitiavsciviliansevts"

global pcwidelayout = "eventspc  battlespc   civilianviolencepc protesteventspc   militaryeventspc  politicalmilitiaeventspc  communalmilitiaeventspc rebelseventspc militaryvspolmilitiaevtspc  polmilitiavsciviliansevtspc "
global levelwidelayout = "events   battles   civilianviolence  protestevents militaryevents  politicalmilitiaevents  communalmilitiaevents rebelsevents  militaryvspolmilitiaevts  polmilitiavsciviliansevts"
global logpcwidelayout = "logeventspc logbattlespc logcivilianviolencepc logprotesteventspc logmilitaryeventspc  logpoliticalmilitiaeventspc  logcommunalmilitiaeventspc logrebelseventspc  logmilitaryvspolmilitiaevtspc  logpolmilitiavsciviliansevtspc"


global cluster ="ID_lgc tm"


